/**
 * 通用下拉树控件
 * @author wonderful-Wu DaoHong
 * @create 20150710
 * @change 新增下拉数据查询功能 wonderful 20161220
 * @change 修改部分缺陷 wonserful 20170918
 */

//定义公共事件绑定状态判断，防止事件重复绑定
var isSelDocClick=false;
var isPanelAndConentScroll=false;
var isWindowScroll=false;
var isDocMouseDown=false;
var isHideSels=false;
var isSearchInputKeyUp=false;
(function(){
	/*var opt={
			id:"testWdfSel",             //渲染对象ID
			className:"wdfSel",          //样式名称
			zTreeOption:treeSetting,     //zTree配置参数
			mulitValsChecked:true,       //是否为多值选择模式
			isReadOnly:true,             //是否是只读
			width:200,                   //下拉面板的宽度
			height:200,                  //下拉面板高度
			zIndex:9999,                 //下面板层层级
			searchBar:{                  //搜索栏定义
			isOpen:true,                 //是否开启
			isInitBindData:true,         //是否初始化控件上已绑定数据,默认为是。
			title:"",                    //标题
			dataCache:"selsData",        //定义一个数据缓存的key名称，控件会把数据从服务端获取后缓存在该key里面，
			                             //所有使用该sel参数生成的下拉控件，都会从该key直接获取缓存数据，也就是说，定义该参数后，只会向服务端请求一次数据。
			position:{                   //默认下来面板定位的位移值设置，单位为px。
			  leftMove:10,               //x轴的位移
			  topMove:-10                //y轴的位移
			},
			},
			data:[],                     //要绑定到控件的数据，如果有该参数，则不会触发向服务端请求数据
			dataModel:{                  //后端对象到下拉树节点对象字段映射,如果定义该参数
				id:"id",//节点id           //则控件值域获取到的值将是realID映射字段的值
				realID:"realID",//业务id
				pId:"pId",//父节点id
				name:"name"//显示字段
			},
			valInput:{                   //控件值域的名称和ID用于表单的值提交
				id:"",
				name:""
			},
			isFixedHeight:true,          //是否固定下拉面板高度
			treeDataUrl:"",             //非异步模式的树控件数据获取地址
			param:{//要提交的参数
				
			},
			dataFilter:function(nodes){  //如果非异步模式的树控件数据过滤操作
				
			},
			async: {                      //异步加载
		        enable: true,
		        url: "http://host/getNode.php",
		        autoParam: ["realID=pID"]
	         },
			event:{
				onChange:function(inputObj,selObj){
			    
				},
				onZtreeInit:function(selObj,inputObj,inputVal){
			    	
			    },
				onZTreeLoaded:function(zTree,selObj){
			    	
			    },
			    onNodeClick:function(treeNode,inputObj){
			    //单选模式节点单击前执行，如果返回false则节点不会执行填充数据操作
			    return true;
			    },
			    onOkBtnClick:function(treeNodes,inputObj){
			    //多选模式下该函数获取要填充到值域的所有树节点，如果要求某些节点不被包含，
			    //则在该节点打上标记如：treeNode.nin=true
			    $(treeNodes).each(function(i,obj){
			    if(obj.isParent){
			    obj.nin=true;
			    }
			    });
			    },
			    onNodeCheck:function(event, treeId, treeNode) {
                     alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
                },
                onAllCheck:function(treeObj) {
                     alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
                }
			}
	  };*/

	
	
	//事件传播处理
	function stopBubble(e)  
	   {  
	       if (e && e.stopPropagation)  
	           e.stopPropagation(); 
	       else 
	           window.event.cancelBubble=true;
	   };
	   //选择面板宽度初始化
	   function initSelWidth(opt,selPanel,inputObj){
		   if(opt.width){
				selPanel.css({width:opt.width});
			}else{
		   		if(opt.autoWidthType == true){
                    selPanel.css({width:"auto"});
                    selPanel.css({minWidth:inputObj.outerWidth()});
				}else{
                    selPanel.css({width:inputObj.outerWidth()});
				}

			}
				if(opt.isFixedHeight){
					if(opt.searchBar&&opt.searchBar.isOpen){
						selPanel.height(opt.height+49);
					}else{
						selPanel.height(opt.height);
					}
					
				}

	   }
	  
	$.fn.extend({"wdfSel":function(method,originalOpt){
		var opt={};
		$.extend(true,opt,originalOpt);
		
		if(opt.zTreeOption=="default"){
			
			opt.zTreeOption={
					data : {
						simpleData : {
							enable : true
						}
					},
					check: {
						enable: true,
						chkStyle:"checkbox",
						chkboxType: { "Y": "p", "N": "s" }
					},
					async:{
						enable:false
					},
					callback : {
						//beforeDrag : beforeDrag,
						//beforeEditName : beforeEditName,
						//beforeRemove : beforeRemove,
						//beforeRename : beforeRename,
						//onRemove : onRemove,
						//onRename : onRename,
						//onClick : zTreeOnClick
					}
				};
		
		}
		
		if(method=="init"){
			
			var inputObj=$(this);
            inputObj.addClass("selInput");
			selDocClick=function(){
                if(!isHideSels) {
                    isHideSels = true;
                    setTimeout(function () {
                        isHideSels = false;
                    }, 1000);
                    $("." + opt.className).css("display", "none");
                }
            };
			panelAndConentScroll=function(){
                if(!isHideSels) {
                    isHideSels = true;
                    setTimeout(function () {
                        isHideSels = false;
                    }, 1000);
                    $("." + opt.className).css("display", "none");
                }
            };
			windowScroll=function(){
			    if(!isHideSels){
			        isHideSels=true;
			    setTimeout(function(){
                    isHideSels=false;
                },1000);

                $("."+opt.className).css("display","none");

                }
            };
			docMouseDown=function(){
                if(!isHideSels) {
                    isHideSels = true;
                    setTimeout(function () {
                        isHideSels = false;
                    }, 1000);
                    $("." + opt.className).css("display", "none");
                }
            };

			if(!opt.mulitValsChecked){
				opt.zTreeOption.check.enable=false;
			}
			if(opt.mulitValsChecked&&opt.isContainParent){
				opt.zTreeOption.check.chkboxType={ "Y": "p", "N": "ps" };
			}else if(opt.mulitValsChecked&&!opt.isContainParent){
				opt.zTreeOption.check.chkboxType={ "Y": "", "N": "ps" };
			}
			
			$("#"+opt.id+"_WdfSel_SelPanel").remove();

			if(!isSelDocClick) {
                $(document).bind("click", selDocClick);
            }
            if(!isPanelAndConentScroll){
			$(".panel .content").bind("scroll",panelAndConentScroll);
            }
            if(!isWindowScroll){
			    $(window).bind("scroll",windowScroll);
            }
            if(!isDocMouseDown){
			    $(document).bind("mousedown",docMouseDown);
            }
			
			/*$(document).unbind("click",inputObj.data("documentClick"));
			inputObj.data("documentClick",function(){
				$(this).find("."+opt.className).css("display","none");
			});
			$(document).bind("click",inputObj.data("documentClick"));
			
			$(window).unbind("resize",inputObj.data("windowResize"));
			inputObj.data("windowResize",function(){
				
			});
			$(window).bind("resize",inputObj.data("windowResize"));
			
			$(".panel .content").unbind("scroll",inputObj.data(".panel.ContentScroll"));
            inputObj.data(".panel.ContentScroll",function(){
				$("."+opt.className).css("display","none");
			});
			$(".panel .content").bind("scroll",inputObj.data(".panel.ContentScroll"));
			
			$(window).unbind("scroll", inputObj.data("windowScroll"));
			 inputObj.data("windowScroll",function(){
				 $("."+opt.className).css("display","none");
				});
			$(window).bind("scroll",inputObj.data("windowScroll"));
			
			$(document).unbind(inputObj.data("mouseDown"));
			 inputObj.data("mouseDown",function(){
				 
					$(this).find("."+opt.className).css("display","none");
					
				});
			$(document).bind("mousedown",inputObj.data("mouseDown"));*/
            if(opt.isReadOnly!=false)
			inputObj.attr("readonly","readonly")
			var inputVal=$("<input id='"+opt.valInput.id+"' type='hidden' name='"+opt.valInput.name+"'/>");
			inputVal.val($("#"+opt.valInput.id).val());
			$("#"+opt.valInput.id).remove();
			inputObj.after(inputVal);
			var selPanel=$("<div id='"+opt.id+"_WdfSel_SelPanel' class='"+opt.className+"'></div>").appendTo("body");
			var treeObj=$("<ul class='ztree' id='"+opt.id+"_WdfSel_ZTree'></ul>").appendTo(selPanel);
			var mutilValsBtn=$("<div class='mutilValsBtn' title='确定'>确定</div>");
			var searchBar;
			var searchInput;
			selPanel.unbind("mouseover");
			selPanel.unbind("mouseout");
			selPanel.unbind("scroll");

			/*
			
			selPanel.bind("mouseout",function(event){
				if($(this).data("mouseOverCount")==1){
					return;
				}
				 var e = event || window.event;
				 var x=e.screenX;
				 var y=e.screenY-67;
				if(x>selPanel.offset().left+selPanel.width()
						||x<selPanel.offset().left
						||y>selPanel.offset().top+selPanel.height()
						||y<selPanel.offset().top){
					
					$(this).css("display","none");
					$(this).data("isMuserOver",false);
				}
				
			});*/
			
			if(opt.searchBar&&opt.searchBar.isOpen){
				
				searchBar=$("<div class='searchBar' title='查询栏'></div>");
				searchInput=$("<input type='text' class='searchInput' placeHolder='输入名称查询'/>").appendTo(searchBar);
				searchBar.appendTo(selPanel);
				treeObj.css("margin-top","53px");
				//异步模式
				if(opt.zTreeOption.async&&opt.zTreeOption.async.enable){
					searchInput.bind("keyup",function(){
						var val=$(this).val();
						if(val&&val!=""){
						function filter(node) {
						    return (node.name.indexOf(val)>-1);
						};
						var zTreeObj=$.fn.zTree.getZTreeObj(treeObj.attr("id"));
						var nodes = zTreeObj.getNodes();
						zTreeObj.hideNodes(nodes);
						var filterNodes=zTreeObj.getNodesByFilter(filter);
						zTreeObj.showNodes(filterNodes);
						}else{
						zTreeObj.showNodes(zTreeObj.getNodes());
						}
					});
			    //非异步模式 	
				}else{
				
				//找到所有叶子节点
				function findLeafNodes(targetNodes,sourceNodes,pId){
					
					$(sourceNodes).each(function(i,obj){
						
						if(obj.pId==pId&&obj.pId&&pId){
							/*if(obj.id=="520100"){
								var _exist=$.inArray(obj.id,targetNodes);
								//去重复
								$(targetNodes).each(function(i){
									if(this.id==obj.id){
										targetNodes.splice(i,1);
									}
								});
								
							}*/
							$(targetNodes).each(function(i){
								if(this.id==obj.id){
									targetNodes.splice(i,1);
								}
							});
							targetNodes.push(obj);
							findLeafNodes(targetNodes,sourceNodes,obj.id);
						}
						
					});
					
					
				}

                    searchInput.bind("keyup", function () {

                        if (!isSearchInputKeyUp) {
                            isSearchInputKeyUp = true;
                            var nodes = inputObj.data("nodesData");
                            var filterNodes = [];
                            var val = $(this).val();

                            setTimeout(function () {
                                isSearchInputKeyUp = false;
                            },10);


                            setTimeout(function () {

                                if (nodes) {
                                    if (val != "") {
                                        $(nodes).each(function (i, obj) {
                                            if (obj.name.indexOf(val) != -1) {
                                                filterNodes.push(obj);
                                            }
                                        });

                                        $(filterNodes).each(function (i, obj) {
                                            findLeafNodes(filterNodes, nodes, obj.id);

                                        });
                                        treeObj.empty();
                                        $.fn.zTree.init(treeObj, opt.zTreeOption, filterNodes);
                                    } else {
                                        treeObj.empty();
                                        $.fn.zTree.init(treeObj, opt.zTreeOption, nodes);
                                    }
                                }
                            }, 500);

                        }
                    });
				}
			}
			
			initSelWidth(opt,selPanel,inputObj);
			selPanel.css({display:"none"});
			
			selPanel.bind("click",function(e){
				stopBubble(e);
			});
			selPanel.bind("mousedown",function(e){
				stopBubble(e);
			});
			selPanel.bind("scroll",function(){
				$(this).find(".searchBar").css("top",$(this).scrollTop()+2);
			});

			var ztreeCheckedNodes;
			//如果是多选状态
			if(opt.mulitValsChecked){
				var checkAllBtn=$("<input type='checkbox' class='checkAllBtn'/>");
				selPanel.append(checkAllBtn);
				selPanel.append(mutilValsBtn);
				checkAllBtn.bind("click",function(){
					var treeObj=$.fn.zTree.getZTreeObj(selPanel.find(".ztree").attr("id"));
					if(treeObj){
                        treeObj.checkAllNodes($(this).is(':checked'));
					}

					if(opt.event.onAllCheck){
						opt.event.onAllCheck(treeObj);
					}
					
				})
				if(opt.isFixedHeight){
				
				/*	treeObj.height(opt.height-mutilValsBtn.outerHeight()-12);
					treeObj.css("overflow","auto");
					treeObj.css("overflow-x","hidden");*/
					
				}
				
				mutilValsBtn.bind("click",function(e){
					var selTree=$.fn.zTree.getZTreeObj(treeObj.attr("id"));
					var nodes=selTree.getCheckedNodes(true);
                    ztreeCheckedNodes = nodes;
					if(opt.event&&opt.event.onOkBtnClick){
						if(nodes)
						opt.event.onOkBtnClick(nodes,inputObj);
					}
					var labelsStr="";
					var valsStr="";
					if(opt.dataModel){
						$(nodes).each(function(i,node){
							if(node.nin){
								if(i+1==nodes.length){
									labelsStr=labelsStr.replace(/\,{1}$/g,"");
									valsStr=valsStr.replace(/\,{1}$/g,"");
								}
								return;
							}
							if(i+1==nodes.length){
								labelsStr+=node[opt.dataModel.name];
								if (opt.dataModel.realID){
                                    valsStr+=node[opt.dataModel.realID];
								}else{
                                    valsStr+=node[opt.dataModel.id];
								}
								return;
							}
							labelsStr+=node[opt.dataModel.name]+",";

                            if (opt.dataModel.realID){
                                valsStr+=node[opt.dataModel.realID]+",";
                            }else{
                                valsStr+=node[opt.dataModel.id]+",";
                            }
						});
						inputVal.val(valsStr);
						inputObj.val(labelsStr);
						inputObj.triggerHandler('change');
						selPanel.css("display","none");
						stopBubble(e);
					}else{
						$(nodes).each(function(i,node){
							if(node.nin){
								if(i+1==nodes.length){
									labelsStr=labelsStr.replace(/\,{1}$/g,"");
									valsStr=valsStr.replace(/\,{1}$/g,"");
								}
								return;
							}
							if(i+1==nodes.length)
								{
								labelsStr+=node.name;
								valsStr+=node.id;
								return;
								}
							labelsStr+=node.name+",";
							valsStr+=node.id+",";
						});
						inputVal.val(valsStr);
						inputObj.val(labelsStr);
						inputObj.triggerHandler('change');
						selPanel.css("display","none");
						stopBubble(e);
					}
					
				});
				
				mutilValsBtn.bind("mousedown",function(e){
					stopBubble(e);
				});
				
			}else{
				if(!opt.zTreeOption["callBack"]){

					opt.zTreeOption["callback"]={
							onClick:function(event, treeId, treeNode) {
								
								if(opt.event&&opt.event.onNodeClick)
									{
									if(!opt.event.onNodeClick(treeNode,inputObj)){
										return;
									}
									}
								if(opt.dataModel){
									if (opt.dataModel.realID){
                                        inputVal.val(treeNode[opt.dataModel.realID]);
									}else{
                                        inputVal.val(treeNode[opt.dataModel.id]);
									}
									inputObj.val(treeNode[opt.dataModel.name]);
								}else if(!opt.dataModel){
									inputVal.val(treeNode.id);
									inputObj.val(treeNode.name);
								}

								inputObj.triggerHandler('change');
								selPanel.css("display","none");
							}
					};
				
				}else{

					opt.zTreeOption.callback.onClick=function(event, treeId, treeNode) {
						
								if(opt.event&&opt.event.onNodeClick)
									{

									if(!opt.event.onNodeClick(treeNode,inputObj)){
										return;
									}
									}
								if(opt.dataModel){

                                    if (opt.dataModel.realID){
                                        inputVal.val(treeNode[opt.dataModel.realID]);
                                    }else{
                                        inputVal.val(treeNode[opt.dataModel.id]);
                                    }
									inputObj.val(treeNode[opt.dataModel.name]);
								}else if(!opt.dataModel){
									inputVal.val(treeNode.id);
									inputObj.val(treeNode.name);
								}
								
								inputObj.triggerHandler('change');
								selPanel.css("display","none");
							};
				
					
				}
				
				if(opt.isFixedHeight){
					/*treeObj.height(opt.height-10);
					treeObj.css("overflow","auto");
					treeObj.css("overflow-x","hidden");*/
				}
				
			}
			
			inputObj.data("inputVal",inputVal);
			inputObj.data("opt",opt);
			inputObj.data("selPanel",selPanel);
			
			inputObj.unbind("click");
			inputObj.unbind("mousedown");
			inputObj.unbind("change");

			inputObj.bind("click",function(e){

				var top=inputObj.offset().top+inputObj.height();
				var left=inputObj.offset().left-1;
				var winWidth=$("body").width();
				var winHeight=$("body").height();
				if(left+selPanel.width()>winWidth){
					left=left-((left+selPanel.width())-winWidth)-10;
				}
				if(top+selPanel.height()>winHeight){
					top=top-((top+selPanel.height())-winHeight)-10;
				}
				var realHeight=inputObj.data("realHeight");
				realHeight=selPanel.height();
				
				var realWidth=inputObj.data("realWidth");
				if(!realHeight){
					realHeight=selPanel.height();
					inputObj.data("realHeight",realHeight);
				}
				if(!realWidth){
					 realWidth=selPanel.width();
					inputObj.data("realHeight",realWidth);
				}
				
				/*selPanel.css({opacity:0,height:0,width:0});
				selPanel.animate({opacity:1},1);
				selPanel.animate({height:realHeight},1);*/
				if(opt.position){
					var pos=opt.position;
					if(pos["topMove"]){
                        top+=pos.topMove;
					}

					if(pos["leftMove"]){
                        left+=pos.leftMove;
					}

				}

				selPanel.css({
					height:realHeight,//预留出10px的横向滚动条高度
					opacity:1,top:top,
					left:left,
					display:"block"});
				initSelWidth(opt,selPanel,inputObj);
                $(".wdfSel").not(selPanel).css("display","none");
				stopBubble(e);
				
			});
			inputObj.bind("mousedown",function(e){
				stopBubble(e);
			});
			//change事件
			if(opt.event&&opt.event.onChange)
			{

				inputObj.bind("change",function(event){
					opt.event.onChange($(this),event,ztreeCheckedNodes);
				});
				 
			}
			//check事件
            if(opt.event&&opt.event.onNodeCheck){
                opt.zTreeOption.callback.onCheck=function(event, treeId, treeNode) {
                    if(opt.event.onCheck){
                	opt.event.onCheck(event, treeId, treeNode);
                    }
                    if(opt.event.onNodeCheck){
                    	opt.event.onNodeCheck(event, treeId, treeNode);
                    }
                    selPanel.show();
                    return;
				}
			}
			
			//非ztree异步模式下的数据填充
			if((!opt.zTreeOption.async||(opt.zTreeOption.async&&!opt.zTreeOption.async.enable))
					&&!opt.data){
				initZTree(treeObj,opt,inputObj);
				var comSelDef=inputObj.attr("comSelDef");
				if(comSelDef&&comSelDef.length>0){
					inputObj.wdfSel("setVal",comSelDef);
				}
			}else if(opt.data){
				loadData(opt,$.extend(true,[],opt.data),inputObj,treeObj);
			}
		}
		
		else if(method=="setVal"){
			
			var inputVal=$(this).data("inputVal");
			if(inputVal == undefined){
				return;
			}
			var selPanel=$(this).data("selPanel");
			var opt=$(this).data("opt");
			var treeObj=$.fn.zTree.getZTreeObj(selPanel.find(".ztree").attr("id"));
			var selObj=$(this);
			var idsArray=[];
			var namesStr="";
			
			selPanel.find(".checkAllBtn").attr("checked",false);
			
			if(originalOpt&&new String(originalOpt).indexOf(",")!=-1){
				idsArray=originalOpt.split(',');
			//避免排除0
			}else if(originalOpt==0||originalOpt!=""&&originalOpt!=null
				&&originalOpt!=undefined&&new String(originalOpt).toUpperCase()!="NULL"){
				
				idsArray.push(originalOpt);
			}else{
				inputVal.val("");
				selObj.val("");
				if(treeObj)
				treeObj.checkAllNodes(false);
				selObj.triggerHandler('change');
				
				return $(this);
			}
			var ztreeOption=opt.zTreeOption;
			if(treeObj)
			treeObj.checkAllNodes(false);
			/*var temp=treeObj.getNodes();
			$(temp).each(function(){
			});*/
			$(idsArray).each(function(i){

				var nodes = null;
				if (opt.dataModel&&opt.dataModel.realID){               
					nodes = treeObj.getNodesByParam("realID",this,null);
                }else{
                    nodes = treeObj.getNodesByParam("id",this,null);
				}
              
				if(nodes&&nodes.length>=1){
					if(ztreeOption.check&&ztreeOption.check.enable)
					treeObj.checkNode(nodes[0],true,false);
					/*if(nodes[0].isParent){
					nodes[0].open=true;
					treeObj.updateNode(nodes[0],false);
					}*/
					if(i+1==idsArray.length){
						namesStr+=nodes[0].name;
						return;
					}
					namesStr+=nodes[0].name+",";
				}
			});
			inputVal.val(originalOpt);
			selObj.val(namesStr);
			selObj.triggerHandler('change');
			return $(this);
		}
        else if(method=="getVal"){
        	var inputVal=$(this).data("inputVal");
			var selPanel=$(this).data("selPanel");
			var opt=$(this).data("opt");
			var treeObj=$.fn.zTree.getZTreeObj(selPanel.find(".ztree").attr("id"));
			var nodes=treeObj.getCheckedNodes(true);
			var datas=[];
			if(opt.dataModel){
				$(nodes).each(function(i,node){
					var data={
						name:node[opt.dataModel.name]
					};
					if (opt.dataModel.realID){
                        data.val=node[opt.dataModel.realID];
					}else{
                        data.val=node[opt.dataModel.id];
					}
					datas.push(data);
				});
			}else{
				$(nodes).each(function(i,node){
					var data={
						val:node["id"],
						name:node["name"]
					};
					datas.push(data);
				});
		}
			 return datas;
        }}
	    });
	
	function initZTree(treeObj,opt,inputObj){
		
		if(opt.event&&opt.event.onZtreeInit){
			var selObj=inputObj.data("selPanel");
			var inputVal=inputObj.data("inputVal");
			opt.event.onZtreeInit(opt,selObj,inputObj,inputVal);
		}
		//参数空字符串控制
		if(opt.param){
		for(var key in opt.param){
			if(opt.param[key]==""){
				delete opt.param[key];
			}
		}
		}

		if(opt.dataCache&&$("body").data(opt.dataCache)){
              var data=$("body").data(opt.dataCache);
              loadData(opt,data,inputObj,treeObj);
        }else if(opt.dataCache&&!$("body").data(opt.dataCache)){
            var ajaxOpt={
                url :opt.treeDataUrl,
                //processData: false,
                //data: xmlDocument,
                async : true,
                dataType : "json",
                type : "POST",
                contentType : "application/x-www-form-urlencoded",
                data :opt.param,
                beforeSend : null,
                success : function(data) {
                    loadData(opt,data,inputObj,treeObj);
                    $("body").data(opt.dataCache,data);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {

                }
            };
            $.ajax(ajaxOpt);
        }else {

            var ajaxOpt={
                url :opt.treeDataUrl,
                //processData: false,
                //data: xmlDocument,
                async : true,
                dataType : "json",
                type : "POST",
                contentType : "application/x-www-form-urlencoded",
                data :opt.param,
                beforeSend : null,
                success : function(data) {
                    loadData(opt,data,inputObj,treeObj);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {

                }
            };
            $.ajax(ajaxOpt);

        }

	}
	
	function loadData(opt,data,inputObj,treeObj){
		
		var zTreeNodes=[];
		var rootData=null;
		if(opt.root&&!opt.data){
			if(data[opt.root]&&data[opt.root].length==1&&!data[opt.root][0]){
			rootData=[];
			}else{
			rootData=data[opt.root];
			}
			
		}else{
			if(data&&data.length==1&&!data[0]){
				rootData=[];
				}else{
				rootData=data;
				}
		}
		
		if(opt.dataModel){
			$(rootData).each(function(){
				var node={};
				node.id=this.id;
				node.pId=this[opt.dataModel.pId];
				node.name=this[opt.dataModel.name];
				node.realID=this[opt.dataModel.realID];
				node.nodeType=this[opt.dataModel.nodeType];
				zTreeNodes.push(node);
			});
		}
		else{
			zTreeNodes=rootData;
		}
		
		if(opt.dataFilter)
		{
			opt.dataFilter(zTreeNodes);
		}
		
		inputObj.data("nodesData",zTreeNodes);
		
		if((!opt.isOpen&&opt.isOpen!=false)||opt.isOpen==true){
			$(zTreeNodes).each(function(i,obj){
				obj.open=true;
			});
		}
	
	    var	zTree=$.fn.zTree.init(treeObj,opt.zTreeOption,zTreeNodes);
	    if((!opt.isInitBindData&&opt.isInitBindData!=false)||opt.isInitBindData)
        inputObj.wdfSel("setVal","");
		if(opt.event&&opt.event.onZTreeLoaded)
		   opt.event.onZTreeLoaded(zTree,inputObj,zTreeNodes);
		
	}
	
})()